<?php

namespace App\Http\Controllers\Entity\Asset\Video\Media;

use App\Http\Controllers\Controller;
use App\Http\Requests\Entity\Asset\Video\Media\VifileRequest;
use App\Models\Common\EntityData\Channel\Sp;
use App\Models\Common\Enumdata\File\Video\Vfileext;
use App\Models\Common\Enumdata\Video\Resolution;
use App\Models\Entity\Asset\Video\Media\Vifile;
use App\Models\Entity\Asset\Video\Vasseti;
use App\Models\Entity\Asset\Video\Vseriali;
use App\Repositories\Entity\Asset\Video\Media\VifileRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;
use Validator;
use DB;

class VifileController extends Controller
{
    use ModelForm;

    /** @var  VifileRepository */
    private $vifileRepository;

    public function __construct(VifileRepository $vifileRepo)
    {
        $this->vifileRepository = $vifileRepo;
    }

    /**
     * Display a listing of the Vifile.
     *
     * @return Response
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.index_header'));
            $content->description(__('entity.asset.video.media.vifiles.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new Vifile.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.create_header'));
            $content->description(__('entity.asset.video.media.vifiles.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vifile in storage.
     *
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function store(VifileRequest $request)
    {
        $input = $request->all();

        $vifile = $this->vifileRepository->create($input);

        Flash::success(__('entity.asset.video.media.vifiles.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.media.vifiles.index'));

    }

    /**
     * Display the specified Vifile.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        return view('entity.asset.video.media.vifiles.show')->with('vifile', $vifile);

    }

    /**
     * Show the form for editing the specified Vifile.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.video.media.vifiles.edit_header'));
            $content->description(__('entity.asset.video.media.vifiles.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vifile in storage.
     *
     * @param  int              $id
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        $sValidators = [
            'sp_id'         => 'required|max:10',
            'resolution_id' => 'required|max:100',
            'vfileext_id'   => 'required|max:100',
            'title'         => 'max:100',
            'subtitle'      => 'max:100',
            'name'          => 'required|max:100',
            'path'          => 'required|max:100',
            // 'size'          => 'required',
            // 'md5'           => 'required',
            'duration'      => 'required',
            'href'          => 'required|max:200',
            'short_desc'    => 'required|max:100',
            'type'          => 'required',
            'alias_name'    => 'required|max:100',
            'listorder'     => 'required|numeric',
        ];
        $validators = [];

        $input = $request->all();

        // 判断是否行内编辑
        $input = $this->handleEditable($input);

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }
        // 定义校验, 没有移到request
        $validator = Validator::make($input, $validators, [
            // 'required' => ':attribute 不能为空',
        ], [
            'sp_id'         => '服务商',
            'resolution_id' => '清晰度',
            'vfileext_id'   => '文件扩展名',
            'title'         => '标题',
            'subtitle'      => '副标题',
            'name'          => '名称',
            'alias_name'    => '别名',
            'path'          => '文件路径',
            'duration'      => '时长',
            'href'          => '下载路径',
            'type'          => '文件类型',
            'short_desc'    => '描述',
        ]
        );

        // 校验
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        $vifile = $this->vifileRepository->update($input, $id);

        Flash::success(__('entity.asset.video.media.vifiles.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.media.vifiles.index'));
    }

    /**
     * Remove the specified Vifile from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vifileRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        // 如果被其他地方引用则不能删除
        $vassetiVifiles = DB::table('vasseti_vifiles')->where(['vifile_id' => $id])->first();

        if ($vassetiVifiles) {
            return response()->json(['message' => '有剧集包含该文件, 不能删除!', 'status' => false]);
        }

        // 如果被其他地方引用则不能删除
        $vserialiVifiles = DB::table('vseriali_vifiles')->where(['vifile_id' => $id])->first();

        if ($vserialiVifiles) {
            return response()->json(['message' => '有分集包含该文件, 不能删除!', 'status' => false]);
        }

        if ($flag = $this->vifileRepository->delete($id)) {

            return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * 剧集文件crud
     */
    /**
     * Display a listing of the Vifile.
     *
     * @return Response
     */
    public function vassetiIndex($vasseti_id)
    {

        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.index_header'));
            $content->description(__('entity.asset.video.media.vifiles.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new Vifile.
     *
     * @return Response
     */
    public function vassetiCreate($vasseti_id)
    {
        // 如果剧集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vasseti::where('id', '=', $vasseti_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该剧集已上线发布,不能新增视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
        }

        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.create_header'));
            $content->description(__('entity.asset.video.media.vifiles.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vifile in storage.
     *
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function vassetiStore($vasseti_id, VifileRequest $request)
    {
        // 如果剧集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vasseti::where('id', '=', $vasseti_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该剧集已上线发布,不能新增视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
        }

        // 新建的时候和剧集关联
        if (!$vasseti = Vasseti::find($vasseti_id)) {
            Flash::error('剧集不能为空!');
            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        $input  = $request->all();
        $vifile = $this->vifileRepository->create($input);

        $vasseti->vifiles()->save($vifile);

        Flash::success(__('entity.asset.video.media.vifiles.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));

    }

    /**
     * Show the form for editing the specified Vifile.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vassetiEdit($vasseti_id, $id)
    {
        // 如果剧集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vasseti::where('id', '=', $vasseti_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该剧集已上线发布,不能修改视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
        }

        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.video.media.vifiles.edit_header'));
            $content->description(__('entity.asset.video.media.vifiles.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vifile in storage.
     *
     * @param  int              $id
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function vassetiUpdate($vasseti_id, $id, Request $request)
    {
        // 如果剧集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vasseti::where('id', '=', $vasseti_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该剧集已上线发布,不能修改视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
        }

        $sValidators = [
            'sp_id'         => 'required|max:10',
            'resolution_id' => 'required|max:100',
            'vfileext_id'   => 'required|max:100',
            'title'         => 'max:100',
            'subtitle'      => 'max:100',
            'name'          => 'required|max:100',
            'path'          => 'required|max:100',
            // 'size'          => 'required',
            // 'md5'           => 'required',
            'duration'      => 'required',
            'href'          => 'required|max:200',
            'short_desc'    => 'required|max:100',
            'type'          => 'required',
            'alias_name'    => 'required|max:100',
            'listorder'     => 'required|numeric',
        ];
        $validators = [];

        $input = $request->all();

        // 判断是否行内编辑
        $input = $this->handleEditable($input);

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }
        // 定义校验, 没有移到request
        $validator = Validator::make($input, $validators, [
            // 'required' => ':attribute 不能为空',
        ], [
            'sp_id'         => '服务商',
            'resolution_id' => '清晰度',
            'vfileext_id'   => '文件扩展名',
            'title'         => '标题',
            'subtitle'      => '副标题',
            'name'          => '名称',
            'path'          => '文件路径',
            'duration'      => '时长',
            'href'          => '下载路径',
            'alias_name'    => '别名',
            'type'          => '文件类型',
            'short_desc'    => '描述',
        ]
        );

        // 校验
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        $vifile = $this->vifileRepository->update($input, $id);

        Flash::success(__('entity.asset.video.media.vifiles.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
    }

    /**
     * Remove the specified Vifile from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vassetiDestroy($vasseti_id, $id)
    {
        // 如果剧集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vasseti::where('id', '=', $vasseti_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该剧集已上线发布,不能修改视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vassetis.vasseti_details', ['vasseti_id' => $vasseti_id]));
        }

        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vifileRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        if ($flag = $this->vifileRepository->delete($id)) {
            // 如果使用模型关系的话
            // // 移除用户身上某一身份...
            // $user->roles()->detach($roleId);
            DB::table('vasseti_vifiles')->where(['vifile_id' => $id, 'vasseti_id' => $vasseti_id])->delete();

            return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * 分集文件crud
     */
    /**
     * Display a listing of the Vifile.
     *
     * @return Response
     */
    public function vserialiIndex($vseriali_id)
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.index_header'));
            $content->description(__('entity.asset.video.media.vifiles.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new Vifile.
     *
     * @return Response
     */
    public function vserialiCreate($vseriali_id)
    {
        // 如果分集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vseriali::where('id', '=', $vseriali_id)->select(['online_status', 'publish_status'])->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该分集已上线发布,不能新增视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
        }

        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.video.media.vifiles.create_header'));
            $content->description(__('entity.asset.video.media.vifiles.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vifile in storage.
     *
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function vserialiStore($vseriali_id, VifileRequest $request)
    {
        // 如果分集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vseriali::where('id', '=', $vseriali_id)->select(['online_status', 'publish_status'])->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该分集已上线发布,不能新增视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
        }

        // 新建的时候和剧集关联
        if (!$vseriali = Vseriali::find($vseriali_id)) {
            Flash::error('分集不能为空!');
            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        $input  = $request->all();
        $vifile = $this->vifileRepository->create($input);

        $vseriali->vifiles()->save($vifile);

        Flash::success(__('entity.asset.video.media.vifiles.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));

    }

    /**
     * Show the form for editing the specified Vifile.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vserialiEdit($vseriali_id, $id)
    {
        // 如果分集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vseriali::where('id', '=', $vseriali_id)->select(['online_status', 'publish_status'])->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该分集已上线发布,不能修改视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
        }

        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.video.media.vifiles.edit_header'));
            $content->description(__('entity.asset.video.media.vifiles.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vifile in storage.
     *
     * @param  int              $id
     * @param VifileRequest $request
     *
     * @return Response
     */
    public function vserialiUpdate($vseriali_id, $id, Request $request)
    {
        // 如果分集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vseriali::where('id', '=', $vseriali_id)->select(['online_status', 'publish_status'])->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该分集已上线发布,不能修改视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
        }

        $sValidators = [
            'sp_id'         => 'required|max:10',
            'resolution_id' => 'required|max:100',
            'vfileext_id'   => 'required|max:100',
            'title'         => 'max:100',
            'subtitle'      => 'max:100',
            'name'          => 'required|max:100',
            'alias_name'    => 'required|max:100',
            'path'          => 'required|max:100',
            // 'size'          => 'required',
            // 'md5'           => 'required',
            'duration'      => 'required',
            'href'          => 'required|max:200',
            'short_desc'    => 'required|max:100',
            'type'          => 'required',
            'listorder'     => 'required|numeric',
        ];
        $validators = [];

        $input = $request->all();

        // 判断是否行内编辑
        $input = $this->handleEditable($input);

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }
        // 定义校验, 没有移到request
        $validator = Validator::make($input, $validators, [
            // 'required' => ':attribute 不能为空',
        ], [
            'sp_id'         => '服务商',
            'resolution_id' => '清晰度',
            'vfileext_id'   => '文件扩展名',
            'title'         => '标题',
            'subtitle'      => '副标题',
            'name'          => '名称',
            'alias_name'    => '别名',
            'path'          => '文件路径',
            'duration'      => '时长',
            'href'          => '下载路径',
            'type'          => '文件类型',
            'short_desc'    => '描述',
        ]
        );

        // 校验
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        $vifile = $this->vifileRepository->update($input, $id);

        Flash::success(__('entity.asset.video.media.vifiles.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
    }

    /**
     * Remove the specified Vifile from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vserialiDestroy($vseriali_id, $id)
    {
        // 如果分集已经上线且发布,则不让新增、编辑、删除操作
        $data = Vseriali::where('id', '=', $vseriali_id)->select(['online_status', 'publish_status'])->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error('该分集已上线发布,不能删除视频文件');

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.video.vserialis.vseriali_details', ['vseriali_id' => $vseriali_id]));
        }

        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vifileRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vifile = $this->vifileRepository->findWithoutFail($id);

        if (empty($vifile)) {
            Flash::error(__('entity.asset.video.media.vifiles.not_found'));

            return redirect(route('entity.asset.video.media.vifiles.index'));
        }

        if ($flag = $this->vifileRepository->delete($id)) {
            DB::table('vseriali_vifiles')->where(['vifile_id' => $id, 'vseriali_id' => $vseriali_id])->delete();

            return response()->json(['message' => __('entity.asset.video.media.vifiles.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(Vifile::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('title', '标题');
            $form->text('subtitle', '副标题');
            $Sps = Sp::pluck('name', 'id');
            $form->select('sp_id', '服务商')->options($Sps);
            $Resolutions = Resolution::pluck('name', 'id');
            $form->select('resolution_id', '清晰度')->options($Resolutions);
            $Vfileexts = Vfileext::pluck('name', 'id');
            $form->select('vfileext_id', '文件扩展名')->options($Vfileexts);
            $form->text('name', '名称')->help('必填');
            $form->text('alias_name', '别名')->help('必填');
            $form->text('path', '文件路径')->help('必填');
            $form->number('size', '文件大小(M)');
            $form->text('md5', '文件MD5');
            $form->number('duration', '时长');
            $form->text('href', '下载路径')->help('必填');
            $form->text('short_desc', '简述')->help('必填');
            $form->textarea('long_desc', '详述');
            $form->radio('type', '文件类型')->options(array(
                1 => '正片',
                2 => '片花',
                3 => '预告',
                4 => '其他',
            ))->default('1');
            $form->number('listorder', '排序');
            // $form->display('created_at', __('base.created_at'));
            // $form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid()
    {

        return Admin::grid(Vifile::class, function (Grid $grid) {
            // 考虑是否需要scope和排序
            // $grid->model()->orderBy('listorder', 'asc');

            // //添加按钮
            // if (!\Gate::check('entity.asset.video.media.vifiles.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     //编辑按钮
            //     if (!\Gate::check('entity.asset.video.media.vifiles.edit')) {
            //         $actions->disableEdit();
            //     }
            //     //删除按钮
            //     if (!\Gate::check('entity.asset.video.media.vifiles.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // //导出按钮
            // if (!\Gate::check('entity.asset.video.media.vifiles.export')) {
            //     $grid->disableExport();
            // }

            // //批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         //批量删除按钮
            //         if (!\Gate::check('entity.asset.video.media.vifiles.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            //添加按钮
            if (Admin::user()->cannot('entity.asset.video.media.vifiles.create')) {
                $grid->disableCreation();
            }

            // 编辑和删除按钮
            $grid->actions(function ($actions) {
                //编辑按钮
                if (Admin::user()->cannot('entity.asset.video.media.vifiles.edit')) {
                    $actions->disableEdit();
                }
                //删除按钮
                if (Admin::user()->cannot('entity.asset.video.media.vifiles.destroy')) {
                    $actions->disableDelete();
                }
            });

            //导出按钮
            if (Admin::user()->cannot('entity.asset.video.media.vifiles.export')) {
                $grid->disableExport();
            }

            //批量操作
            $grid->tools(function ($tools) {

                $url =  route('entity.assets.assets.model',['sp_no' => '','config_file_select'=>'']);
                $tools->append('<a href=' .$url. ' class="btn btn-sm btn-warning pull-right" title="导入数据"><i class="fa fa-ban"></i>导入数据</a>');

                $tools->batch(function ($batch) {
                    //批量删除按钮
                    if (Admin::user()->cannot('entity.asset.video.media.vifiles.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '名称')->editable();
            $grid->column('alias_name', '别名')->editable();
            $grid->column('sp.name', '服务商')->sortable();
            $grid->column('resolution.name', '清晰度')->sortable();
            $grid->column('vfileext.name', '文件扩展名')->sortable();
            $grid->column('title', '标题')->editable();
            // $grid->column('subtitle', '副标题')->sortable();
            // $grid->column('path', '文件路径')->sortable();
            $grid->column('size', '文件大小(M)')->sortable();
            $grid->column('md5', '文件MD5')->sortable();
            $grid->column('duration', '时长')->sortable();
            // $grid->column('href', '下载路径')->sortable();
            $grid->column('type', '文件类型')->display(function ($val) {
                switch ($val) {
                    case 1:
                        return '正片';
                        break;
                    case 2:
                        return '片花';
                        break;
                    case 3:
                        return '预告';
                        break;
                    default:
                        return '其他';
                        break;
                }
            });
            $grid->column('short_desc', '简述')->sortable();
            // $grid->column('long_desc', '详述')->editable();
            $grid->column('listorder', '排序')->sortable()->editable();
            // 详情
            $grid->column('详情')->expand(function () {
                $data              = [];
                $data['subtitle']  = $this->subtitle;
                $data['path']      = $this->path;
                $data['href']      = $this->href;
                $data['long_desc'] = $this->long_desc;
                return view('entity.asset.video.media.vifile.details', $data);
            }, '详情');

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();
                // 清晰度
                $tempResolutions = Resolution::pluck('name', 'id');
                $filter->equal('resolution_id', '清晰度')->select($tempResolutions);
                // 文件扩展名
                $tempVfileexts = Vfileext::pluck('name', 'id');
                $filter->equal('vfileext_id', '文件扩展名')->select($tempVfileexts);
                // 服务商
                $tempSps = Sp::pluck('name', 'id');
                $filter->equal('sp_id', '服务商')->select($tempSps);
                // 文件类型
                $aTypes = array(
                    1 => '正片',
                    2 => '片花',
                    3 => '预告',
                    4 => '其他',
                );
                $filter->equal('type', '文件类型')->select($aTypes);
                // 文件编号
                $filter->like('vifile_no', '文件编号');
                // 名称
                $filter->like('name', '名称');
                // 标题
                $filter->like('title', '标题');
                // 副标题
                $filter->like('subtitle', '副标题');
                // 文件大小(M)
                $filter->between('size', '文件大小(M)');
                // 文件MD5
                $filter->like('md5', '文件MD5');
                // 时长
                $filter->between('duration', '时长');
            });
        });
    }
}
